import 'dart:ui';

import 'package:flutter_bmfmap_example/pages/marketpage/pages/shoppingCart/buyCar.dart';
import 'package:provider/provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bmfmap_example/models/marketinfomodel.dart';
import 'package:flutter_bmfmap_example/pages/marketpage/pages/searchpage/searchpage.dart';
import 'package:flutter_bmfmap_example/service/marketservice.dart';
import 'package:flutter_bmfmap_example/tool/HYSizeFit%20.dart';
import 'package:flutter_svg/svg.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

import 'componets/goodsSwiper.dart';
import 'componets/goodslistview.dart';

// import 'componets/goodsSwiper.dart';

class MarketPage extends StatefulWidget {
  const MarketPage({Key key}) : super(key: key);

  @override
  _MarketPageState createState() => _MarketPageState();
}

class _MarketPageState extends State<MarketPage>
    with SingleTickerProviderStateMixin {
  List<Widget> banner = [
    // GoodsSwiper(),
    // GoodsSwiper(),
    // GoodsSwiper(),
    // GoodsSwiper(),
  ]; //swiper 数组

  var _tabController;

  _getbannerlist() async {
    var result = await Marketservice.getbannerlist();
    print(result);
    for (int i = 0; i < result.length; i++) {
      banner.add(
        GoodsSwiper(
          id: result[i]['id'],
          title: result[i]['title'],
          mtitle: result[i]['mtitle'],
          url: result[i]['url'],
          tagname: 'swiper' + i.toString(),
        ),
      );
    }
    setState(() {});
  }

  List<String> getDataList() {
    List<String> list = [];
    for (int i = 0; i < 10; i++) {
      list.add(i.toString());
    }
    return list;
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tabController = TabController(vsync: this, length: 4);
    _getbannerlist();
  }

  @override
  Widget build(BuildContext context) {
    List<String> datas = [];
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: _buildsearchbar(),
        centerTitle: true,
        automaticallyImplyLeading: false,
        actions: [
          InkWell(
            onTap: () {
              Navigator.of(context)
                  .push(MaterialPageRoute(builder: (context) => BuycarPage()));
            },
            child: Container(
              height: 40,
              width: 40,
              child: Stack(
                children: [
                  Container(
                    child: Center(
                      child: SvgPicture.asset(
                        'assets/images/icons/svg/购物车.svg',
                        color: Colors.white,
                        width: 32,
                      ),
                    ),
                  ),
                  Positioned(
                    left: 24,
                    top: 8,
                    child: Container(
                      width: 16,
                      height: 16,
                      decoration: new BoxDecoration(
                        color: Color.fromRGBO(255, 20, 20, 1),
                        //设置四周圆角 角度
                        borderRadius: BorderRadius.all(Radius.circular(10.0)),
                      ),
                      child: Center(
                        child: Text(
                          context
                              .read<Shoppingcatmodel>()
                              .getshoppingcattotalnum()
                              .toString(),
                          style: TextStyle(
                              fontSize: 10, fontWeight: FontWeight.w700),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
          SizedBox(
            width: 12,
          ),
        ],
        backgroundColor: Color.fromRGBO(200, 150, 224, 1),
        elevation: 0,
      ),
      body: Column(
        children: [
          Container(
            // height: 200,
            // width: 300,
            color: Color.fromRGBO(200, 150, 224, 1),
            child: Stack(
              children: [
                Positioned(
                  // top: 10,
                  bottom: -30,
                  child: Container(
                    height: 120,
                    width: HYSizeFit.screenWidth,
                    decoration: new BoxDecoration(
                      color: Colors.white,
                      //设置四周圆角 角度
                      borderRadius: BorderRadius.all(Radius.circular(32.0)),
                    ),
                    // child: Text('data'),
                  ),
                ),
                // Positioned(),
                _buildbanner(),
              ],
            ),
          ),
          _buildtabbar(),
          Expanded(
            child: TabBarView(
              controller: _tabController,
              children: <Widget>[
                GoodsListview(
                  type: '全部',
                ),
                GoodsListview(type: '家电'),
                GoodsListview(type: '穿戴'),
                GoodsListview(type: '家居'),
              ],
            ),
          ),
        ],
      ),
    );
  }

  _buildtabbar() {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: TabBar(
        controller: _tabController,
        //选中条大小
        indicatorSize: TabBarIndicatorSize.label,
        //选中字体颜色
        labelColor: Color.fromRGBO(123, 122, 123, 1),
        //选项条颜色
        indicatorColor: Color.fromRGBO(193, 194, 196, 1),
        //未选中字体颜色
        unselectedLabelColor: Color.fromRGBO(123, 122, 123, 0.5),

        labelStyle: TextStyle(fontWeight: FontWeight.bold),
        tabs: <Widget>[
          Tab(text: "全部"),
          Tab(text: "家电"),
          Tab(text: "穿戴"),
          Tab(text: "家居"),
        ],

        // 记得要带上tabController
      ),
    );
  }

  Widget getItemContainer(String item) {
    return Container(
      width: 15.0,
      height: 5.0,
      alignment: Alignment.center,
      child: Text(
        item,
        style: TextStyle(color: Colors.white, fontSize: 20),
      ),
      color: Colors.blue,
    );
  }

  _buildbanner() {
    return Container(
      padding: EdgeInsets.all(8.0),
      height: 175,
      child: Swiper(
        itemBuilder: (BuildContext context, int index) {
          //条目构建函数传入了index,根据index索引到特定图片
          return banner[index];
        },
        itemCount: banner.length,
        itemWidth: HYSizeFit.screenWidth*0.86,
        layout: SwiperLayout.STACK,
        // autoplay: true,
      ),
    );
  }

  _buildsearchbar() {
    return GestureDetector(
      onTap: () {
        Navigator.push(
            context, MaterialPageRoute(builder: (context) => SearchPage()));
      },
      child: Container(
        child: Row(
          // mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            SizedBox(
              width: 12,
            ),
            Icon(
              Icons.search,
              color: Colors.black26,
            ),
            Padding(
              padding: const EdgeInsets.only(left: 8),
              child: Text(
                '请输入搜索内容',
                style: TextStyle(
                  fontSize: 16,
                  color: Color.fromRGBO(187, 187, 188, 1),
                ),
              ),
            ),
          ],
        ),
        height: 36,
        //搜索框
        decoration: new BoxDecoration(
          color: Color.fromRGBO(255, 255, 255, 1),
          //设置四周圆角 角度
          borderRadius: BorderRadius.all(Radius.circular(18.0)),
        ),
      ),
    );
  }
}
